<?php
class omeanalysts_mdl_ome_reshipreturntime extends dbeav_model{

    //var $has_export_cnf = true;

    var $export_name = '售后退货表(按过数时间)';

    public function table_name($real=false){
        $table_name = 'ome_delivery';
        if($real){
            return kernel::database()->prefix.$table_name;
        }else{
            return $table_name;
        }
    }

    public function count($filter=null){

        $where = $this->_filter($filter); 

        $sql = "SELECT distinct os.order_id,a.t_begin,a.t_begin t_appro,a.reship_bn,a.return_type,os.order_bn,d.axorderno,f.`name` shop_name,sb.`name` src_branch,bh.`name` return_branch,a.is_check,a.leemantype,a.send_status,os.createtime,
                  a.ax_returnid,a.ax_returnorderbn ax_returnno,d.axshipno,a.returnreason, cp.name return_logi_name,a.logi_no,a.return_logi_no,a.ship_name ship,a.ship_area,a.ship_addr,
                  item.bn,ext.specifications spec,ext.unit,items.number buynumber,item.num number,a.totalmoney returnamoney,rep.content returncontent,a.memo,a.AXCustomer,a.Re_AXCustomer,
                  case when users.name is null then '系统' else users.name end  operator,item.price,a.inter_time
                  from sdb_ome_reship a
                  inner join sdb_ome_reship_items item on a.reship_id=item.reship_id 
                  inner JOIN sdb_ome_orders os on os.order_id=a.order_id
                  left JOIN sdb_ome_delivery_order odo on (odo.order_id = os.order_id)
                  left JOIN sdb_ome_delivery od on ( od.delivery_id = odo.delivery_id )
                  left join sdb_ome_shop f on a.shop_id=f.shop_id
                  left join sdb_wms_delivery e on ( e.outer_delivery_bn = od.delivery_bn )
                  inner join sdb_wms_delivery_items items on e.delivery_id=items.delivery_id and items.bn=item.bn
                  LEFT JOIN sdb_wms_delivery_bill bill ON   bill.logi_no=a.logi_no
                  INNER JOIN sdb_wms_deliverynote_line d ON d.delivery_bn = e.delivery_bn AND bill.delivery_id=d.delivery_id
                  left JOIN sdb_material_basic_material_ext ext on item.product_id=ext.bm_id 
                  left join sdb_ome_branch bh on a.branch_id=bh.branch_id
                  left join sdb_ome_branch sb on e.branch_id=sb.branch_id
                  LEFT JOIN  sdb_pam_account op on op.account_id=a.op_id
                  left join sdb_desktop_users users on users.user_id = op.account_id
                  left join sdb_ome_return_product rep on rep.return_bn=a.reship_bn
                  left join sdb_ome_dly_corp cp on a.return_logi_name=cp.corp_id
                  where ".$where." ";


        $row = $this->db->select($sql);
        return count($row);
    }

    function getdividefeenew($order_id,$bn)
    {
      $sql="SELECT  items.bn,sum(items.divide_order_fee) divide_order_fee,sum(items.nums) nums from sdb_ome_orders os 
              inner join sdb_ome_order_items items on os.order_id=items.order_id
              where os.order_id='$order_id' and items.bn='$bn' and items.delete='false' 
              group by items.bn ";
      $rows = $this->db->select($sql);
      return $rows;
    }
    public function getlist($cols='*', $filter=array(), $offset=0, $limit=-1, $orderType=null){
  

        if(!$filter['time_from'] && !$filter['time_to']){
            return false;
        }else{

            $where = $this->_filter($filter); 

           // var_dump($where);
          $sql = "SELECT distinct os.order_id,a.t_begin,a.t_begin t_appro,a.reship_bn,a.return_type,os.order_bn,d.axorderno,f.`name` shop_name,sb.`name` src_branch,bh.`name` return_branch,a.is_check,a.leemantype,a.send_status,os.createtime,
                  a.ax_returnid,a.ax_returnorderbn ax_returnno,d.axshipno,a.returnreason, a.return_logi_name return_logi_name,a.logi_no,a.return_logi_no,a.ship_name ship,a.ship_area,a.ship_addr,
                  item.bn,ext.specifications spec,ext.unit,items.number buynumber,item.num number,a.totalmoney returnamoney,rep.content returncontent,a.memo,a.AXCustomer,a.Re_AXCustomer,
                  case when users.name is null then '系统' else users.name end  operator,item.price,a.inter_time
                  from sdb_ome_reship a
                  inner join sdb_ome_reship_items item on a.reship_id=item.reship_id 
                  inner JOIN sdb_ome_orders os on os.order_id=a.order_id
                  left JOIN sdb_ome_delivery_order odo on (odo.order_id = os.order_id)
                  left JOIN sdb_ome_delivery od on ( od.delivery_id = odo.delivery_id )
                  left join sdb_ome_shop f on a.shop_id=f.shop_id
                  left join sdb_wms_delivery e on ( e.outer_delivery_bn = od.delivery_bn )
                  inner join sdb_wms_delivery_items items on e.delivery_id=items.delivery_id and items.bn=item.bn
                  LEFT JOIN sdb_wms_delivery_bill bill ON   bill.logi_no=a.logi_no
                  INNER JOIN sdb_wms_deliverynote_line d ON d.delivery_bn = e.delivery_bn AND bill.delivery_id=d.delivery_id
                  left JOIN sdb_material_basic_material_ext ext on item.product_id=ext.bm_id 
                  left join sdb_ome_branch bh on a.branch_id=bh.branch_id
                  left join sdb_ome_branch sb on e.branch_id=sb.branch_id
                  LEFT JOIN  sdb_pam_account op on op.account_id=a.op_id
                  left join sdb_desktop_users users on users.user_id = op.account_id
                  left join sdb_ome_return_product rep on rep.return_bn=a.reship_bn
                  where ".$where." ";
            $rows = $this->db->selectLimit($sql,$limit,$offset);
          
            foreach ($rows as $key => $value) 
            {
                $divide_order_fee=$this->getdividefeenew($value['order_id'],$value['bn']);
                $rows[$key]['price'] = ($divide_order_fee[0]['divide_order_fee']/$divide_order_fee[0]['nums']);
            }
            return $rows;
        }
        
    }
    
    public function searchOptions(){
        $parentOptions = parent::searchOptions();
        $childOptions = array(
            'reship_bn'=>app::get('base')->_('订单号'),
        );
        return $Options = array_merge($parentOptions,$childOptions);
    }

    public function _filter($filter,$tableAlias=null,$baseWhere=null){


        $where = array(1);

       $where[]=" a.return_type in('return','change','onlyreturn') ";
       if(empty($filter['time_from']))
        {
          $where[]=' 1=2 ';
        }
        if(isset($filter['time_from']) && $filter['time_from']){
            $where[] = ' a.inter_time >='.strtotime($filter['time_from']);
        }
        if(isset($filter['time_to']) && $filter['time_to']){
            $where[] = ' a.inter_time <'.(strtotime($filter['time_to'])+86399);
        }
         
        if(isset($filter['shop_id'])   && $filter['shop_id']){
            $where[] = ' f.shop_id  =\''.addslashes($filter['shop_id']).'\'';
        }
        if(!$filter['shop_id'] && $filter['shop_ids']){
            $where[] = ' f.shop_id IN (\''.implode('\',\'', $filter['shop_ids']).'\')';
        }
        unset($filter['shop_id']);
         if(isset($filter['AXCustomer']) && $filter['AXCustomer']){
            $where[]= ' d.ax_shopname  =\''.addslashes($filter['AXCustomer']).'\'';

        }  
        if(isset($filter['axcustomercode']) && $filter['axcustomercode']){
            $where[]= ' f.AXCustomer  =\''.addslashes($filter['axcustomercode']).'\'';

        }
        if(isset($filter['order_bn']) && $filter['order_bn']){
            $where[] = ' os.order_bn =\''.addslashes($filter['order_bn']).'\'';
        }

        if(isset($filter['branch'])   && $filter['branch']){
            $where[] = ' bh.branch_id  =\''.addslashes($filter['branch']).'\'';
        }
        if(!$filter['branch'] && $filter['branch']){
            $where[] = ' bh.branch_id IN (\''.implode('\',\'', $filter['branch']).'\')';
        }
        unset($filter['branch']);

        if(isset($filter['p_bn']) && $filter['p_bn']){
            $where[] = ' item.bn =\''.addslashes($filter['p_bn']).'\'';
        }
        if(isset($filter['reship_bn']) && $filter['reship_bn']){
            $where[] = ' a.reship_bn =\''.addslashes($filter['reship_bn']).'\'';
        }
        if(isset($filter['ax_returnid']) && $filter['ax_returnid']){
            $where[] = ' a.ax_returnid =\''.addslashes($filter['ax_returnid']).'\'';
        }
        if(isset($filter['ax_bn']) && $filter['ax_bn']){
            $where[] = ' a.ax_bn =\''.addslashes($filter['ax_bn']).'\'';
        }
      
        if($filter['status']=='1' ){
             $where[] = " a.send_status ='0' ";
        }else if ($filter['status'] =='2'){
             $where[] = " a.send_status ='1' ";
        } 
        if(isset($filter['is_check']) && $filter['is_check'] && $filter['is_check']=='14' )
        {
            $where[] = ' a.is_check =\''.addslashes($filter['is_check']).'\'';
        }
      

        return implode($where,' AND ');
    }

    public function get_schema(){
        $schema = array (
            'columns' => array (
                't_begin' =>
                array (
                  'type' => 'time',
                  'label' => '单据创建日期',
                  'comment' => '单据创建日期',
                  'editable' => false,
                  'in_list' => true,
                ),
                't_appro' =>
                array (
                  'type' => 'time',
                  'label' => '单据审核时间',
                  'comment' => '单据审核时间',
                  'editable' => false,
                  'in_list' => true,
                ),
                'reship_bn' =>
                array (
                  'type' => 'varchar(255)',
                  'label' => '退换货单号',
                  'editable' => false,
                  'searchtype' => 'has',
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                  'width' => '120',
                ),
                'return_type' =>
                array (
                  'type' =>
                  array (
                    'return' => '退货',
                    'change' => '换货',
                    'refuse' => '拒收退货',
                    'nosrc' => '无来源退货',
                    'onlyreturn' => '仅退货',
                  ),
                  'default' => 'return',
                  'required' => true,
                  'comment' => '换货类型',
                  'editable' => false,
                  'label' => '换货类型',
                  'width' => '60',
                ),
                'order_bn' =>
                array (
                  'type' => 'varchar(255)',
                  'label' => '订单号',
                  'editable' => false,
                  'searchtype' => 'has',
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                  'width' => '100',
                ),
                'axorderno' =>
                array (
                  'type' => 'varchar(255)',
                  'label' => 'AX销售订单号',
                  'editable' => false,
                  'searchtype' => 'has',
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                  'width' => '100',
                ),
                'shop_name' =>
                array (
                  'type' => 'varchar(255)',
                  'label' => '店铺',
                  'editable' => false,
                  'searchtype' => 'has',
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                  'width' => '100',
                ),
                
                'src_branch' =>
                array (
                  'type' => 'varchar(255)',
                  'label' => '来源仓库',
                  'editable' => false,
                  'searchtype' => 'has',
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                  'width' => '100',
                ),
                'return_branch' =>
                array (
                  'type' => 'varchar(255)',
                  'label' => '退回仓库',
                  'editable' => false,
                  'searchtype' => 'has',
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                  'width' => '100',
                ),
                'is_check' =>
                array(
                  'type' => array(
                    0 => '未审核',
                    1 => '审核成功',
                    2 => '审核失败',
                    3 => '收货成功',
                    4 => '拒绝收货',
                    5 => '拒绝',
                    6 => '补差价',
                    7 => '完成',
                    8 => '质检通过',
                    9 => '拒绝质检',
                    10 => '质检异常',
                    11 => '待确认',
                    12=>'收货异常',
                    13 => '质检中',
                    14=>'过数关闭'
                  ),
                  'default'=>'0',
                  'comment' => '当前状态',
                  'editable' => false,
                  'label' => '当前状态',
                  'width' =>65,
                  'in_list' => true,
                  'default_in_list' => true,
                  'width' => '60',
                ),
                'leemantype' =>
                  array (
                  'type' => 'table:shop@ome',
                  'label' => '理文店铺类型',
                  'width' => 75,
                  'editable' => false,
                  'in_list' => true,
                  'filtertype' => 'normal',
                ),
                'send_status' =>
                array(
                  'type' => array(
                    0 => '未同步',
                    1 => '已同步',
                  ),
                  'default'=>'0',
                  'comment' => '同步erp状态',
                  'editable' => false,
                  'label' => '同步erp状态',
                  'in_list' => true,
                  'default_in_list' => true,
                  'width' => '60',
                ),
                'createtime' =>
                array (
                  'type' => 'time',
                  'label' => '订单创建时间',
                  'comment' => '订单创建时间',
                  'editable' => false,
                  'in_list' => true,
                ),
                'ax_returnid' =>
                array (
                  'type' => 'varchar(255)',
                  'label' => '退回AX授权号',
                  'editable' => false,
                  'searchtype' => 'has',
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                  'width' => '100',
                ),
                'ax_returnno' =>
                array (
                  'type' => 'varchar(255)',
                  'label' => 'AX退货订单号',
                  'editable' => false,
                  'searchtype' => 'has',
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                  'width' => '100',
                ),
                'axshipno' =>
                array (
                  'type' => 'varchar(255)',
                  'label' => 'AX送货单号',
                  'editable' => false,
                  'searchtype' => 'has',
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                  'width' => '100',
                ),
                'returnreason' =>
                array(
                  'type' =>'varchar(320)',
                  'comment' => '退货退款原因',
                  'editable' => false,
                  'label' => '退货退款原因',
                  'in_list' => true,
                  'default_in_list' => true,
                ),
                'return_logi_name' =>
                array (
                  'type' => 'varchar(100)',
                  'label' => '退回物流公司',
                  'comment' => '退回物流公司',
                  'editable' => false,
                  'filtertype' => 'normal',
                  'width' =>75,
                  'in_list' => true,
                  'default_in_list' => true,
                ),
                'return_logi_no' =>
                array (
                  'type' => 'varchar(50)',
                  'label' => '退回物流单号',
                  'comment' => '退回物流单号',
                  'editable' => false,
                  'searchtype' => 'has',
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'width' =>130,
                  'in_list' => true,
                  'default_in_list' => true,
                ),
                'logi_no' =>
                array (
                  'type' => 'varchar(50)',
                  'label' => '原物流单号',
                  'comment' => '原物流单号',
                  'editable' => false,
                  'searchtype' => 'has',
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'width' =>130,
                  'in_list' => true,
                  'default_in_list' => true,
                ),
                'ship' =>
                array (
                  'type' => 'varchar(30)',
                  'label' => '收货人',
                  'editable' => false,
                  'is_title' => true,
                  'width' => '70',
                ),
                'ship_area' =>
                array (
                  'type' => 'varchar(30)',
                  'label' => '收货地区',
                  'editable' => false,
                  'is_title' => true,
                ),
                'ship_addr' =>
                array (
                  'type' => 'varchar(30)',
                  'label' => '收货地址',
                  'editable' => false,
                  'is_title' => true,
                ),
                'bn' =>
                array (
                  'type' => 'varchar(30)',
                  'label' => '物料编码',
                  'editable' => false,
                  'is_title' => true,
                ),
                'spec'=>
                array(
                    'type'=>'varchar(32)',
                    'label'=>'规格',
                    'in_list' => true,
                    'default_in_list' => true,
                  'width' => '50',
                ),
                'unit' =>
                array (
                  'type' => 'varchar(200)',
                  'required' => true,
                  'label' => '单位',
                  'is_title' => true,
                  'default_in_list' => true,
                  'width' => 70,
                  'searchtype' => 'has',
                  'editable' => false,
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                  'width' => '50',
                ),
                'buynumber' =>
                array (
                  'type' => 'number',
                  'label' => '购买量',
                  'required' => true,
                  'default' => 0,
                  'editable' => false,
                  'width' => '50',
                ),
                'number' =>
                array (
                  'type' => 'number',
                  'label' => '退货量',
                  'required' => true,
                  'default' => 0,
                  'editable' => false,
                  'width' => '50',
                ),
                'returnamoney' =>
                array (
                  'type' => 'money',
                  'label' => '退款金额',
                  'comment' => '退款金额',
                  'editable' => false,
                  'filtertype' => 'normal',
                  'in_list' => true,
                  'width' => '70',
                ),
                'returncontent' =>
                array(
                  'type' =>'varchar(320)',
                  'comment' => '申请售后原因',
                  'editable' => false,
                  'label' => '申请售后原因',
                  'in_list' => true,
                  'default_in_list' => true,
                ),
                'memo' =>
                array(
                  'type' =>'varchar(320)',
                  'comment' => '备注',
                  'editable' => false,
                  'label' => '备注',
                  'in_list' => true,
                  'default_in_list' => true,
                ),
                'AXCustomer' =>
                array (
                 'type' => 'varchar(255)',
                 'label' => 'AX客户编码',
                 'editable' => false,
                 'searchtype' => 'has',
                 'filtertype' => 'normal',
                 'filterdefault' => true,
                 'in_list' => true,
                 'default_in_list' => true,
                 'is_title' => true,
                 'width' => '120',
                ),
                'Re_AXCustomer' =>
                array (
                 'type' => 'varchar(255)',
                 'label' => 'AX退货客户编码',
                 'editable' => false,
                 'searchtype' => 'has',
                 'filtertype' => 'normal',
                 'filterdefault' => true,
                 'in_list' => true,
                 'default_in_list' => true,
                 'is_title' => true,
                 'width' => '120',
                ),

                'operator'=>
                array(
                    'type'=>'varchar(50)',
                    'label'=>'操作员',
                    'in_list' => true,
                    'default_in_list' => true,
                  'width' => '50',
                ),
                'price' =>
                array (
                  'type' => 'money',
                  'label' => '实付金额',
                  'comment' => '实付金额',
                  'editable' => false,
                  'filtertype' => 'normal',
                  'in_list' => true,
                  'width' => '70',
                ),
                'inter_time' =>
                array (
                  'type' => 'time',
                  'label' => '过数时间',
                  'comment' => '过数时间',
                  'editable' => false,
                  'in_list' => true,
                ),
                
            ),
            'idColumn' => 'name',
            'in_list' => array (
                0 => 't_begin',
                1 => 't_appro',
                2 => 'reship_bn',
                3 => 'return_type',
                4 => 'order_bn',
                5 => 'axorderno',
                6 => 'shop_name',
                7 => 'src_branch',
                8 => 'return_branch',
                9 => 'is_check',
                10 => 'leemantype',
                11 => 'send_status',
                12 => 'createtime',
                13 => 'ax_returnid',
                14 => 'ax_returnno',
                15 => 'axshipno',           
                16 => 'returnreason',     
                17 => 'return_logi_name',     
                18 => 'return_logi_no',     
                19 => 'logi_no',       
                20 => 'ship',     
                21 => 'ship_area',     
                22 => 'ship_addr',     
                23 => 'bn',     
                24 => 'spec',     
                25 => 'unit',     
                26 => 'buynumber',     
                27 => 'number',     
                28 => 'returnamoney',
                29 => 'returncontent', 
                30 => 'memo',    
                31 => 'AXCustomer',
                32 => 'Re_AXCustomer',
                33 => 'operator', 
                34 => 'price', 
                35 => 'inter_time',      

            ),
            'default_in_list' => array (
                0 => 't_begin',
                1 => 't_appro',
                2 => 'reship_bn',
                3 => 'return_type',
                4 => 'order_bn',
                5 => 'axorderno',
                6 => 'shop_name',
                7 => 'src_branch',
                8 => 'return_branch',
                9 => 'is_check',
                10 => 'leemantype',
                11 => 'send_status',
                12 => 'createtime',
                13 => 'ax_returnid',
                14 => 'ax_returnno',
                15 => 'axshipno',           
                16 => 'returnreason',     
                17 => 'return_logi_name',     
                18 => 'return_logi_no',     
                19 => 'logi_no',       
                20 => 'ship',     
                21 => 'ship_area',     
                22 => 'ship_addr',     
                23 => 'bn',     
                24 => 'spec',     
                25 => 'unit',     
                26 => 'buynumber',     
                27 => 'number',     
                28 => 'returnamoney',
                29 => 'returncontent', 
                30 => 'memo',    
                31 => 'AXCustomer',
                32 => 'Re_AXCustomer',
                33 => 'operator', 
                34 => 'price', 
                35 => 'inter_time',                                  
            ),
        );
        return $schema;
    }

    /**
     * 获得日志类型(non-PHPdoc)
     * @see dbeav_model::getLogType()
     */
    public function getLogType($logParams) {
        $type = $logParams['type'];
        $logType = 'none';
        if ($type == 'export') {
            $logType = $this->exportLogType($logParams);
        }
        elseif ($type == 'import') {
            $logType = $this->importLogType($logParams);
        }
        return $logType;
    }
    /**
     * 导出日志类型
     * @param Array $logParams 日志参数
     */
    public function exportLogType($logParams) {
        $params = $logParams['params'];
        $type = 'report';
        if ($logParams['app'] == 'omeanalysts' && $logParams['ctl'] == 'ome_delivery') {
            $type .= '_purchaseReport_expressAnalysis';
        }
        $type .= '_export';
        return $type;
    }
    /**
     * 导入操作日志类型
     * @param Array $logParams 日志参数
     */
    public function importLogType($logParams) {
        $params = $logParams['params'];
        $type = 'report';
        if ($logParams['app'] == 'omeanalysts' && $logParams['ctl'] == 'ome_delivery') {
            $type .= '_purchaseReport_expressAnalysis';
        }
        $type .= '_import';
        return $type;
    }

    function io_title( $filter, $ioType='csv' ){


        switch( $filter ){
            case 'delivery':
                $this->oSchema['csv'][$filter] = array(
                    '*:单据创建时间' => 't_begin',
                    '*:单据审核时间' => 't_appro',
                    '*:退换货单号' => 'reship_bn',
                    '*:退换货类型' => 'return_type',
                    '*:订单号' => 'order_bn',
                    '*:AX销售订单' => 'axorderno',
                    '*:来源店铺' => 'shop_name',
                    '*:来源仓库' => 'src_branch',
                    '*:退回仓库' => 'return_branch',
                    '*:当前状态' => 'is_check',
                    '*:理文店铺类型' => 'leemantype',
                    '*:同步ERP状态' => 'send_status',
                    '*:订单创建时间' => 'createtime',
                    '*:退回AX授权号' => 'ax_returnid',
                    '*:AX退货订单号' => 'ax_returnno',
                    '*:AX送货单号' => 'axshipno',           
                    '*:退货原因' => 'returnreason',     
                    '*:退回物流公司' => 'return_logi_name',     
                    '*:退回物流单号' => 'return_logi_no',     
                    '*:原物流单号' => 'logi_no',
                    '*:收货人' => 'ship',     
                    '*:收货地区' => 'ship_area',     
                    '*:收货地址' => 'ship_addr',     
                    '*:基础物料编码' => 'bn',     
                    '*:规格' => 'spec',     
                    '*:单位' => 'unit',     
                    '*:购买量' => 'buynumber',     
                    '*:退货量' => 'number',     
                    '*:退款金额' => 'returnamoney', 
                    '*:申请售后原因' => 'returncontent',
                    '*:备注' => 'memo',
                    '*:AX客户编码' => 'AXCustomer',
                    '*:AX退货客户编码' => 'Re_AXCustomer',
                    '*:操作员' => 'operator',
                    '*:实付单价' => 'price',
                    '*:过数时间' => 'inter_time',
                    
                );
                break;
        }
        $this->ioTitle[$ioType][$filter] = array_keys( $this->oSchema[$ioType][$filter] );
        return $this->ioTitle[$ioType][$filter];
    }

    //根据查询条件获取导出数据
    public function getExportDataByCustom($fields, $filter, $has_detail, $curr_sheet, $start, $end){
          //根据选择的字段定义导出的第一行标题
          if($curr_sheet == 1){

              $title = array();
              foreach($this->io_title('delivery') as $k => $v ){
                  $title[] = mb_convert_encoding($v, 'GBK', 'UTF-8');
              }

              $data['content']['main'][0] = '"'.implode('","',$title).'"';
          }
          $is_check=array(
                  0 => '未审核',
                  1 => '审核成功',
                  2 => '审核失败',
                  3 => '收货成功',
                  4 => '拒绝收货',
                  5 => '拒绝',
                  6 => '补差价',
                  7 => '完成',
                  8 => '质检通过',
                  9 => '拒绝质检',
                  10 => '质检异常',
                  11 => '待确认',
                  12=>'收货异常',
                  13 => '质检中',
                  14=>'过数关闭'
                );
          $return_type=array (
              'return' => '退货',
              'change' => '换货',
              'refuse' => '拒收退货',
              'nosrc' => '无来源退货',
              'onlyreturn' => '仅退货',
            );

          $where = $this->_filter($filter);
          $sql = "SELECT distinct os.order_id,a.t_begin,a.t_begin t_appro,a.reship_bn,a.return_type,os.order_bn,d.axorderno,f.`name` shop_name,sb.`name` src_branch,bh.`name` return_branch,a.is_check,a.leemantype,a.send_status,os.createtime,
                  a.ax_returnid,a.ax_returnorderbn ax_returnno,d.axshipno,a.returnreason, cp.name return_logi_name,a.return_logi_no,a.logi_no,a.ship_name ship,a.ship_area,a.ship_addr,
                  item.bn,ext.specifications spec,ext.unit,items.number buynumber,item.num number,a.totalmoney returnamoney,rep.content returncontent,a.memo,a.AXCustomer,a.Re_AXCustomer,
                  case when users.name is null then '系统' else users.name end  operator,item.price,a.inter_time
                  from sdb_ome_reship a
                  inner join sdb_ome_reship_items item on a.reship_id=item.reship_id 
                  inner JOIN sdb_ome_orders os on os.order_id=a.order_id
                  left JOIN sdb_ome_delivery_order odo on (odo.order_id = os.order_id)
                  left JOIN sdb_ome_delivery od on ( od.delivery_id = odo.delivery_id )
                  left join sdb_ome_shop f on a.shop_id=f.shop_id
                  left join sdb_wms_delivery e on ( e.outer_delivery_bn = od.delivery_bn )
                  inner join sdb_wms_delivery_items items on e.delivery_id=items.delivery_id and items.bn=item.bn
                  LEFT JOIN sdb_wms_delivery_bill bill ON   bill.logi_no=a.logi_no
                  inner join sdb_wms_deliverynote_line d ON d.delivery_bn = e.delivery_bn AND bill.delivery_id=d.delivery_id
                  left JOIN sdb_material_basic_material_ext ext on item.product_id=ext.bm_id 
                  left join sdb_ome_branch bh on a.branch_id=bh.branch_id
                  left join sdb_ome_branch sb on e.branch_id=sb.branch_id
                  LEFT JOIN  sdb_pam_account op on op.account_id=a.op_id
                  left join sdb_desktop_users users on users.user_id = op.account_id
                  left join sdb_ome_return_product rep on rep.return_bn=a.reship_bn
                  left join sdb_ome_dly_corp cp on a.return_logi_name=cp.corp_id
                  where ".$where." ";

            $omedelivery = $this->db->selectLimit($sql,$end,$start);

            error_log(date('Y-m-d H:i:s').$sql."".$start."".$end."".PHP_EOL, 3, DATA_DIR.'/logs/reshipreturntime'.date('Y-m-d').'.log');
            foreach ($omedelivery as $key => $value) 
            {
                $omedelivery[$key]['t_begin'] =date('Y-m-d h:i:s',$value['t_begin']);
                $omedelivery[$key]['t_appro'] =date('Y-m-d h:i:s',$value['t_appro']);
                $omedelivery[$key]['return_type'] =$return_type[$value['return_type']];
                
                $omedelivery[$key]['is_check'] =$is_check[$value['is_check']];
                if($value['send_status']=='0')
                {
                  $omedelivery[$key]['send_status'] ='未同步';
                }else{
                  $omedelivery[$key]['send_status'] ='已同步';
                }
                $divide_order_fee=$this->getdividefeenew($value['order_id'],$value['bn']); 
                error_log(date('Y-m-d H:i:s').'-'.$key.'开始'.$value['order_bn'].'-'.$value['bn'].'【'.json_encode($divide_order_fee)."】".PHP_EOL, 3, DATA_DIR.'/logs/reshipreturntime'.date('Y-m-d').'.log');
                $omedelivery[$key]['price'] = ($divide_order_fee[0]['divide_order_fee']/$divide_order_fee[0]['nums']);
                unset($omedelivery[$key]['order_id']);
                $omedelivery[$key]['inter_time'] =date('Y-m-d h:i:s',$value['inter_time']);
                
            }

            foreach ($omedelivery as $k => $aFilter) {
                $exptmp_data = array();
                foreach ($aFilter as $col => $val) {
                    $aFilter[$col] = mb_convert_encoding($aFilter[$col], 'GBK', 'UTF-8');
                    $exptmp_data[] = $aFilter[$col];
                }
                $data['content']['main'][] = implode(',', $exptmp_data);
            }
            return $data;
        
    }
}
